package com.cykj.pos.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.domain.FeiShunCategory;
import com.cykj.pos.mapper.FeiShunCategoryMapper;
import com.cykj.pos.util.FeiShunUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("feishun" )
@Api(tags = "派达薪接口")
public class feishunController {

    private final FeiShunCategoryMapper feiShunCategoryMapper;

    /**
     * 派达薪登录接口
     * @return
     */
    @PostMapping("/feishunLogon")
    public AjaxResult feishunLogon(){
        AjaxResult ajaxResult=AjaxResult.success("派达薪登录");
        Map map = new HashMap<>();
        map.put("username","吉林省龙之付科技管理有限公司");
        map.put("password","e10adc3949ba59abbe56e057f20f883e");
        map.put("remark","company");
        map.put("is",1);
        try {
            String data= FeiShunUtil.doPost("http://lhyg.bjpdx.com/api/auth/login",null,JSONObject.toJSONString(map));
            JSONObject obj = JSONObject.parseObject(data);
            String code = obj.getString("code");
            String token = obj.getString("data");
            JSONObject accessTokenObj = JSONObject.parseObject(token);
            String tokenq=accessTokenObj.getString("access_token");
            String msg = obj.getString("msg");
            FeiShunCategory vo= feiShunCategoryMapper.getFeiShunTask("02001");
            map.put("content","银付通提现任务");//任务内容
            map.put("shareTypeCode",vo.getClassId());//二级分类 Id
            map.put("shareTypePCode",vo.getPid());//一级分类 Id
            map.put("title","银付通提现任务");//任务名称
//            JSONObject balanceData= FeiShunUtil.send("/open/openTaskRsa/getBalance",tokenq,null);//查询余额接口
            JSONObject resData= FeiShunUtil.send("/open/openTaskRsa/addTask",tokenq,JSONObject.toJSONString(map));
            String rescode = resData.getString("code");
//            String d = balanceData.getString("code");
            //TODO 存储导数据库，保存登录成功或失败的原因
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ajaxResult;
    }
    /**
     * 获取任务列表
     * @return
     */
    @PostMapping("/getTaskList")
    public AjaxResult getTaskList(){
        AjaxResult ajaxResult=AjaxResult.success("获取任务列表");
        try {
            JSONObject resData= FeiShunUtil.send("/open/openDriRsa/dir",null,null);
            String code = resData.getString("code");
            if("200".equals(code)){
                String data = resData.getString("data");
                JSONArray arr= (JSONArray) JSONArray.parse(data);
                for (Object obj:arr) {
                    JSONObject jsonObject= (JSONObject) JSON.toJSON(obj);
                    FeiShunCategory vo = new FeiShunCategory();
                    vo.setName(jsonObject.getString("name"));
                    vo.setPid("0");
                    vo.setClassId(jsonObject.getString("id"));
                    vo.setClassGrade("0");
                    feiShunCategoryMapper.insert(vo);
                    //插入二级类目
                    String twoString=jsonObject.getString("list");
                    if(null!=twoString&&!"".equals(twoString)){
                        JSONArray arrTwo=(JSONArray)JSONArray.parse(twoString);
                        for (Object objTwo:arrTwo){
                            JSONObject jsonTwo= (JSONObject) JSON.toJSON(objTwo);
                            FeiShunCategory twoVo = new FeiShunCategory();
                            twoVo.setName(jsonTwo.getString("name"));
                            twoVo.setPid(jsonTwo.getString("pid"));
                            twoVo.setClassId(jsonTwo.getString("id"));
                            twoVo.setClassGrade("1");
                            feiShunCategoryMapper.insert(twoVo);
                        }
                    }
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ajaxResult;
    }

    /**
     * 创建任务
     * @return
     */
    @PostMapping("/addTaskList")
    public AjaxResult addTaskList(){
        AjaxResult ajaxResult=AjaxResult.success("创建任务");
        Map map = new HashMap<>();
        FeiShunCategory vo= feiShunCategoryMapper.getFeiShunTask("02001");
        map.put("content","众亿付提现任务");//任务内容
        map.put("shareTypeCode",vo.getClassId());//二级分类 Id
        map.put("shareTypePCode",vo.getPid());//一级分类 Id
        map.put("title","众亿付提现任务");//任务名称
        try {
            JSONObject resData= FeiShunUtil.send("/open/openTaskRsa/addTask",null,JSONObject.toJSONString(map));
            String code = resData.getString("code");
            if("200".equals(code)){
                JSONObject obj= JSONObject.parseObject(resData.getString("data"));
                String taskId = obj.getString("taskId");
            }

            //TODO 存储导数据库，保存登录成功或失败的原因
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ajaxResult;
    }
    /**
     * 申请任务（主要是提交结算信息）
     * @return
     */
    @PostMapping("/applyTaskList")
    public AjaxResult applyTaskList(){
        AjaxResult ajaxResult=AjaxResult.success("申请任务");
        Map map = new HashMap<>();
        map.put("taskId","a697649f6150494ebe0bfa34ca0d2c5a");//任务id
        List settleUserVos=new ArrayList();//申请人集合
        for(int i=0;i<=1;i++){
            Map listMap = new HashMap<>();
            listMap.put("bankName","招商银行");//申请人开户银行
            listMap.put("bankNum","6321310101018157525");//申请人银行账户
            listMap.put("settleAccount","10");//金额(元)
            listMap.put("userName","孟鑫");//申请人名称
            listMap.put("userNum","220724199701021439");//申请人身份证号
            listMap.put("userTel","18844809026");//申请人手机号
            listMap.put("bankSubName","");//申请人开户支行 非必填
            listMap.put("remark","");//透传参数  非必填
            settleUserVos.add(listMap);
        }
        map.put("settleUserVos",settleUserVos);
        try {
            JSONObject resData= FeiShunUtil.send("/open/openTaskRsa/initiateSettlement",null,JSONObject.toJSONString(map));
            String code = resData.getString("code");
            if("200".equals(code)){
                JSONObject obj= JSONObject.parseObject(resData.getString("data"));
                String cpayCount = obj.getString("cpayCount");//需打款金额
                JSONArray userVos=obj.getJSONArray("userVos");
                for(Object m:userVos){
                    Map mapVo= (Map) m;
                    mapVo.get("userNum");//身份证号
                    mapVo.get("taskUserId");//结算ID
                    mapVo.get("remark");//透传参数
                }
            }
            //TODO 存储导数据库，保存登录成功或失败的原因
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ajaxResult;
    }
    /**
     * 发起结算
     * @return
     */
    @PostMapping("/initiateSettlement")
    public AjaxResult initiateSettlement(){
        AjaxResult ajaxResult=AjaxResult.success("发起结算");
        Map map = new HashMap<>();
        map.put("settlementId","2b54d8e23e4841db9cbe5bbcc5d1cd2b");//任务结算 id
        try {
            JSONObject resData= FeiShunUtil.send("/open/openTaskRsa/settlement",null,JSONObject.toJSONString(map));
            String code = resData.getString("code");
            //TODO 存储导数据库，保存登录成功或失败的原因
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ajaxResult;
    }
    /**
     * 查询申请人结算状态
     * @return
     */
    @PostMapping("/querySettlement")
    public AjaxResult querySettlement(){
        AjaxResult ajaxResult=AjaxResult.success("查询申请人结算状态");
        List list=new ArrayList();
        list.add("27e2b93df92e4ec6b2d7afa80aa05093");
        list.add("c9a29e3e9d1c4cfea0a456945bcba16a");
        try {
            JSONObject resData= FeiShunUtil.send("/open/openTaskRsa/getSettleUserResult",null,JSONObject.toJSONString(list));
            String code = resData.getString("code");
            //TODO 存储导数据库，保存登录成功或失败的原因
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ajaxResult;
    }
}
